import { WaterSourceAPI } from './api-client.js';

// Cookie请替换为自己的水源社区Cookie
const cookie = 'cookieconsent_status=dismiss; 10.119.9.71:80=22632.59461.21071.0000; _t=f6Y1OX1JddHpgXAhWKyDJU9s2%2FixNJX916%2B9Kfv%2BX7ttidJSDKoXvwzKNYxTpXvPXWEYzwWgze%2Bk6pAVFra%2BJjKxMY%2FD4vmRI0QnsZJkYKOk2RDo%2FujIXrqD9oMXd8aq32YuRAqEv7KJW6kAZtiveMbpeVaEG2LNx6xHOFQ9dpkfAJFkodSi1O6TZyK0MTqMTrb%2BsAJ9Sf5M6R%2F5zb3OFa5cDLESEfatf9PDds6vB%2F0MolpwFlkQ%2FHSe%2F3dbf%2B%2BhSZNU5TQdUz%2B6DiU5XNQIXA1KGfwX%2FeJeYZrk3R%2BWlRqKXlguRLn%2FLgj1caHvzPHzwuEltg%3D%3D--98i7JXsqBw9Qam8X--GkbRAwfaGqDv4yqxJNufrg%3D%3D; _forum_session=m7vBg2U9I4tosY2AyZT7eZ%2BN7dMT9Gd3Oj%2FBzaio7tQORcG8ebPNMbkISl07SAWzzWQTYAVKRIfc4LWY4gB6TyaCUohyWq8wCdqqKAhsTl9e52racGO9pjVmmPAyGY7eYSFnOBTVC3%2BxXO%2FN%2BBML0BEqCuyNBLoWfyXrB3ja1cfw1rVxBnDwHApnuuDTF2nJVZTarmgkiNDuB7PBg3f71nuWMP8XLaCOl8QX9Nlic1XEvkwmk1qp15ax%2FBXLbOu53IYPHhxMBP9%2F4GYzpPxQqlEGbAsF4A%3D%3D--um1eDKTYg4becrR6--ZxKNYh8eZWATzsznLyIvmA%3D%3D';

// 要测试的主题ID
const topicId = '373430';

async function testAPI() {
  try {
    console.log('开始测试API...');
    const api = new WaterSourceAPI(cookie);
    
    // 测试获取主题
    console.log(`\n=== 测试获取主题内容 ${topicId} ===`);
    const topic = await api.getTopic(topicId);
    console.log('主题标题:', topic.title);
    console.log('帖子数量:', topic.posts_count);
    console.log('帖子流中的ID数量:', topic.post_stream.stream.length);
    console.log('已加载的帖子数量:', topic.post_stream.posts.length);
    
    if (topic.post_stream.posts.length > 0) {
      const firstPost = topic.post_stream.posts[0];
      console.log('\n=== 第一个帖子 ===');
      console.log('ID:', firstPost.id);
      console.log('用户名:', firstPost.username);
      console.log('内容预览:', api.extractPostContent(firstPost).substring(0, 200) + '...');
    }
    
    // 测试搜索功能
    const searchQuery = '水源';
    console.log(`\n=== 测试搜索 "${searchQuery}" ===`);
    const searchResults = await api.search(searchQuery);
    console.log('找到主题数量:', searchResults.topics?.length || 0);
    console.log('找到帖子数量:', searchResults.posts?.length || 0);
    
    if (searchResults.topics && searchResults.topics.length > 0) {
      const firstTopic = searchResults.topics[0];
      console.log('\n首个搜索结果:');
      console.log('ID:', firstTopic.id);
      console.log('标题:', firstTopic.title);
      console.log('帖子数量:', firstTopic.posts_count);
    }
    
    // 测试获取多个帖子
    if (topic.post_stream.stream.length > 5) {
      const postIds = topic.post_stream.stream.slice(1, 5).map(id => id.toString());
      console.log(`\n=== 测试获取多个帖子 ${postIds.join(', ')} ===`);
      const posts = await api.getTopicPosts(topicId, postIds);
      console.log('获取到的帖子数量:', posts.length);
      
      if (posts.length > 0) {
        console.log('\n首个获取的帖子:');
        console.log('ID:', posts[0].id);
        console.log('用户名:', posts[0].username);
        console.log('内容预览:', api.extractPostContent(posts[0]).substring(0, 200) + '...');
      }
    }
    
    console.log('\n=== 测试完成 ===');
  } catch (error) {
    console.error('测试失败:', error instanceof Error ? error.message : String(error));
  }
}

testAPI(); 